package es.ull.pfc.server;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;



import org.apache.xerces.parsers.SAXParser;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ParseXML {
	final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";  
	final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";  
	final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
	//TODO Conseguir que lea de una ruta relativa
	final String MY_SCHEMA = "/Users/daniel/Documents/workspace/Test2Graficas/war/Resources/schema.xsd";
	
	private File xmlFile;
	
	public ParseXML (File xml) {
		xmlFile = xml;
	}
	
	public void validarXML () {
		  
		// Creando la factoria e indicando que hay validacion  
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();  
		documentBuilderFactory.setNamespaceAware(true);  
		documentBuilderFactory.setValidating(true);
		          
		try {  
		    
		  //Configurando el Schema de validacion          
		  documentBuilderFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);  
		  documentBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE, new File(MY_SCHEMA));  
		            
		  // Parseando el documento  
		  DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		  documentBuilder.setErrorHandler(new ErrorHandler() {
			
			@Override
			public void warning(SAXParseException arg0) throws SAXException {
				System.out.println("WARNING:" + arg0.getMessage());				
			}
			
			@Override
			public void fatalError(SAXParseException arg0) throws SAXException {
				System.out.println("FATALERROR:" + arg0.getMessage());				
			}
			
			@Override
			public void error(SAXParseException arg0) throws SAXException {
				System.out.println("ERROR:" + arg0.getMessage());				
			}
		});

		  		    
		  
		  //TODO Arreglar esta validacion que no retorna excepcion si no esta validado 
		  Document parse = documentBuilder.parse(this.xmlFile);
		  
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch (IOException ie) {
			ie.printStackTrace(); 
		} catch (Exception ex) {  
			System.out.println("Error");
		}    
	}
	
	
	  
/*	  
	private void parseDocument() {

		//get a factory
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try {
	
			//get a new instance of parser
			SAXParser sp = spf.newSAXParser();
	
			//parse the file and also register this class for call backs
			sp.parse("employees.xml", this);
	
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch (IOException ie) {
			ie.printStackTrace();
		}
	}



	

	//Event Handlers
public void startElement(String uri, String localName, String qName,
	Attributes attributes) throws SAXException {
	//reset
	tempVal = "";
	if(qName.equalsIgnoreCase("Employee")) {
		//create a new instance of employee
		tempEmp = new Employee();
		tempEmp.setType(attributes.getValue("type"));
	}
}


public void characters(char[] ch, int start, int length) throws SAXException {
	tempVal = new String(ch,start,length);
}

public void endElement(String uri, String localName,
	String qName) throws SAXException {

	if(qName.equalsIgnoreCase("Employee")) {
		//add it to the list
		myEmpls.add(tempEmp);

	}else if (qName.equalsIgnoreCase("Name")) {
		tempEmp.setName(tempVal);
	}else if (qName.equalsIgnoreCase("Id")) {
		tempEmp.setId(Integer.parseInt(tempVal));
	}else if (qName.equalsIgnoreCase("Age")) {
		tempEmp.setAge(Integer.parseInt(tempVal));
	}

}





	private void printData(){

	System.out.println("No of Employees '" + myEmpls.size() + "'.");

	Iterator it = myEmpls.iterator();
	while(it.hasNext()) {
		System.out.println(it.next().toString());
	}
}

* 
* 
* 		  
*/	

}
